package PingPong;

import java.awt.Rectangle;

public class Paddle implements Commons {
	public enum Player { Left, Right };
	
	public Paddle( Player p ) {
		init();
		setPlayer( p );
	}
	
	public void init() {
		y = BOARD_HEIGHT / 2 - PADDLE_HEIGHT / 2;
		dy = 0;
		speed = 0;
		acceleration = 10;
		breakForce = -5;
	}
	
	public Player getPlayer() {
		return player;
	}
	
	public int getSpeed() {
		return speed;
	}
	
	public int getY() {
		return y;
	}
	
	public int getDirection() {
		return dy;
	}
	
	public void setY(int y) {
		this.y = y;
	}
	
	public void setSpeed(int s) {
		speed = s;
	}
	
	public void move( long passed ) {
		double passedTime = passed / 100.0;
		
		if( !stop)
			speed += acceleration*passedTime;
		else {
			speed += breakForce*passedTime;
		}
		
		if( speed < 0 )
			speed = 0;
		else if( speed > 50 )
			speed = 50;
		
		y += dy*speed;
		if( y < 0 ) {
			y = 0;
			speed = 0;
		}
		else if( y > BOARD_HEIGHT - PADDLE_HEIGHT ) {
			y = BOARD_HEIGHT - PADDLE_HEIGHT;
			speed = 0;
		}
	}
	
	public void setPlayer( Player p ) {
		player = p;
	}
	
	public void up() {
		dy = -1;
		stop = false;
	}
	
	public void down() {
		dy = 1;
		stop = false;
	}
	
	public void stop() {
		//dy = 0;
		stop = true;
		//speed = 0;
	}

	public Rectangle getRectangle() {
		if( player == Player.Left ) {
			return new Rectangle(-3, y-5, PADDLE_WIDTH+5, PADDLE_HEIGHT+10);
		} else {
			return new Rectangle(BOARD_WIDTH - PADDLE_WIDTH-3, y-5, PADDLE_WIDTH+5, PADDLE_HEIGHT+10);
		}
	}
	
	private int y, dy, speed, acceleration, breakForce;
	private boolean stop = true;
	private Player player;
}
